<template>
  <div>
    <el-dialog
      width="90%"
      :title="$t('卖家客户')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel"
    >
      <el-row>
        <el-form :model="queryParams" ref="queryForm" size="small" inline v-show="showSearch">
          <el-form-item prop="receiveName">
            <el-input
              :placeholder="$t('客户名')"
              v-model="queryParams.receiveName"
              clearable
              @keyup.enter.native="getList"
            />
          </el-form-item>
          <el-form-item prop="offline">
            <el-select
              @change="getList"
              clearable
              :placeholder="$t('是否黑名单')"
              v-model="queryParams.black"
              style="width: 100%"
            >
              <el-option :label="$t('是')" :value="1" />
              <el-option :label="$t('否')" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item prop="receiveName">
            <el-select
              @change="getList"
              clearable
              :placeholder="$t('是否线下客户')"
              v-model="queryParams.offline"
              style="width: 100%"
            >
              <el-option :label="$t('是')" :value="1" />
              <el-option :label="$t('否')" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">
              {{ $t('搜索') }}
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
              {{ $t('重置') }}
            </el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row class="mb8">
        <el-button type="primary" size="mini" @click="handleAdd" v-hasPermi="['distribution:orderClient:add']">
          {{ $t('创建客户') }}
        </el-button>
      </el-row>
      <el-table
        :height="calTableHeight() - 150"
        ref="TableRef"
        :data="tableData"
        @selection-change="(rows) => (tableCheck = rows)"
      >
        <el-table-column show-overflow-tooltip align="center" :label="$t('客户名')" prop="receiveName" />
        <el-table-column show-overflow-tooltip align="center" :label="$t('电话')" prop="receivePhone" />
        <el-table-column show-overflow-tooltip align="center" :label="$t('邮编')" prop="receivePostcode" />
        <el-table-column show-overflow-tooltip align="center" :label="$t('地址1')" prop="address1" />
        <el-table-column show-overflow-tooltip align="center" :label="$t('是否黑名单')" prop="black">
          <template #default="{ row }">
            <!-- black 是否黑名单 0:否 1:是 -->
            <div v-if="row.black == 0" style="color: green">{{ $t('否') }}</div>
            <div v-if="row.black == 1" style="color: red">{{ $t('是') }}</div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip align="center" :label="$t('是否线下客户')" prop="offline">
          <template #default="{ row }">
            <!-- offline	是否线下客户 0:否 1:是 -->
            <div v-if="row.offline == 0" style="color: green">{{ $t('否') }}</div>
            <div v-if="row.offline == 1" style="color: red">{{ $t('是') }}</div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip align="center" :label="$t('操作')">
          <template #default="{ row }">
            <el-dropdown @command="(command) => handleCommand(command, row)">
              <span class="el-dropdown-link">
                {{ $t('更多') }}
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-hasPermi="['distribution:order:list']" command="handleOrderList">
                  {{ $t('查看购买记录') }}
                </el-dropdown-item>
                <!-- black 是否黑名单 0:否 1:是 -->
                <el-dropdown-item
                  v-hasPermi="['distribution:orderClient:mark']"
                  :disabled="row.black == 1"
                  command="mark"
                >
                  {{ $t('标记黑名单') }}
                </el-dropdown-item>
                <el-dropdown-item
                  v-hasPermi="['distribution:orderClient:mark']"
                  :disabled="row.black == 0"
                  command="cancelMark"
                >
                  {{ $t('取消标记黑名单') }}
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <div slot="footer">
        <el-button @click="handleSubmit" type="primary">
          {{ $t('确定') }}
        </el-button>
        <el-button @click="cancel">
          {{ $t('关闭') }}
        </el-button>
      </div>
    </el-dialog>
    <AddAndEdit
      :show.sync="addAndEdit.show"
      @getList="getList"
      :type="addAndEdit.type"
      :formData="addAndEdit.formData"
    />
    <OrderList :show.sync="orderList.show" @getList="getList" :formData="orderList.formData" />
  </div>
</template>
<script>
// import { api } from '@/api/**'
import { orderClientList, mark } from '@/api/platform/sellerManage'
import AddAndEdit from './addAndEdit.vue'
import OrderList from './orderList.vue'
export default {
  components: { AddAndEdit, OrderList },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * type
     */
    type: {
      type: String,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      orderList: {
        show: false,
        formData: {},
      },
      addAndEdit: {
        show: false,
        type: '',
        formData: {},
      },
      /**显示搜索条件 */
      showSearch: true,
      /**表格选中项 */
      tableCheck: [],
      /**表格数据 */
      tableData: [],
      loading: false,
      /**总条数 */
      total: 0,
      /**分页相关查询参数 */
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.getList()
      }
    },
  },
  methods: {
    handleCommand(command, row) {
      this[command](row)
    },
    mark(row) {
      this.$confirm(this.$t('确认标记黑名单?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        const data = {
          id: row.id,
          // 操作类型：0 取消标记 1标记黑名单
          type: 1,
        }
        mark(data).then((res) => {
          const { code, data, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
      })
    },
    cancelMark(row) {
      this.$confirm(this.$t('确认取消标记黑名单?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        const data = {
          id: row.id,
          // 操作类型：0 取消标记 1标记黑名单
          type: 0,
        }
        mark(data).then((res) => {
          const { code, data, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
      })
    },
    handleOrderList(row) {
      this.orderList.formData = { ...row }
      this.orderList.show = true
    },
    handleAdd(row) {
      this.addAndEdit.type = 'add'
      this.addAndEdit.formData = this.formData
      this.addAndEdit.show = true
    },
    handleEdit(row) {
      this.addAndEdit.type = 'edit'
      this.addAndEdit.tformData = { ...row }
      this.addAndEdit.show = true
    },
    handleDelete(row) {
      this.$confirm(this.$t('确认删除?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        const data = {}
        //api(data).then((res) => {
        //const { code, data, msg } = res
        //if (code === 200) {
        // this.$message({ type: 'success', message: msg})
        //this.cancel()
        //this.$emit('getList')
        //}
        //})
      })
    },
    handleSubmit() {
      // TODO
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
        }
      })
      this.cancel()
    },
    getList() {
      this.loading = true
      const data = {
        ...this.queryParams,
        distrId: this.formData.distrId,
      }
      orderClientList(data)
        .then((res) => {
          this.tableData = res.rows
          this.total = res.total
        })
        .finally(() => (this.loading = false))
    },

    cancel() {
      this.$emit('update:show', false)
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = null))
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss"></style>
